package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bean.CmsType1;
import com.woniu.bean.R;
import com.woniu.service.ICmsTypeService1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kk
 * @since 2022-08-06
 */
@RestController
@RequestMapping("/cmsType1")
@CrossOrigin
public class CmsTypeController1 {
    @Autowired
    private ICmsTypeService1 cmsTypeService;
    //分页查询
    @GetMapping
    public R getAll(Integer currentPage, Integer pageSize){
        R r =null;
        IPage iPage =new Page(currentPage,pageSize);
        IPage page = cmsTypeService.page(iPage);
        //每业返回的数据
        List records = page.getRecords();
        //总页数
        long total = page.getTotal();
        //装入map
        Map map =new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        r=R.ok();
        //装入数据
        r.setData(map);
        return r;
    }
    //增加数据
    @PostMapping
    public R saveType(@RequestBody CmsType1 type1){
        R r =null;
        boolean b = cmsTypeService.save(type1);
        if(b){
            r=R.ok();
        }else {
            r=R.error();
        }
        return r;
    }
    //删数据
    @DeleteMapping("/{id}")
    public R deleteType(@PathVariable Integer id){
        boolean b = cmsTypeService.removeById(id);
        R r =null;
        if(b){
            r=R.ok();
        }else {
            r=R.error();
        }
        return r;
    }
    //批量删除
    @DeleteMapping
    public R deleteMore(@RequestBody List<Integer> ids){
        boolean b = cmsTypeService.removeBatchByIds(ids);
        R r =null;
        if(b){
            r=R.ok();
        }else {
            r=R.error();
        }
        return r;
    }

    //修改数据
    @PutMapping
    public  R updateType(@RequestBody CmsType1 cmsType1){
        boolean b = cmsTypeService.updateById(cmsType1);
        R r =null;
        if(b){
            r=R.ok();
        }else {
            r=R.error();
        }
        return r;

    }

    //查id
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id){
        CmsType1 type1 = cmsTypeService.getById(id);
        R r =R.ok();
        r.setData(type1);
        return r;
    }

    //根据名字查
    @GetMapping("name")
    public R getByName(@RequestParam String typeName,Integer currentPage, Integer pageSize ){
        System.out.println("typeName = " + typeName);
        IPage<CmsType1> iPage=new Page<>(currentPage,pageSize);
        QueryWrapper<CmsType1> qr = new QueryWrapper<>();
        if (StringUtils.hasText(typeName)) {
            qr.like("type_name",typeName);
        }
        IPage<CmsType1> page = cmsTypeService.page(iPage, qr);
        List<CmsType1> records = page.getRecords();
        System.out.println("records = " + records);
        long total = page.getTotal();
        R r=R.ok();

        Map map=new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        r.setData(map);
        return r;
    }

}
